﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_UserInfo:IDisposable 
    {
        private SqlDataAdapter dscommand;

        public b_UserInfo()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }

        public b_UserInfodata getuserinfobyID(string username)
        {
            //用用户注册邮箱来获取用户信息
            b_UserInfodata newdata = new b_UserInfodata();
            string selectstring = "SELECT * FROM b_UserInfo WHERE User_ID =" + "'" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_UserInfo");
            tempconn.Close();
            return (newdata);
        }

        public b_UserInfodata getuserinfobyname(string username)
        {
            //输入用户昵称，返回昵称满足条件的用户列表

            b_UserInfodata newdata = new b_UserInfodata();
            string selectstring = "SELECT * FROM b_UserInfo WHERE User_Name =" + "'" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_UserInfo");
            tempconn.Close();
            return (newdata);
        }

        public int countuserbyname(string username)
        {
            //给定用户昵称，返回叫该昵称的用户个数
            //没有则返回0
            return (getuserinfobyname(username).Tables["b_UserInfo"].Rows.Count);
        }



        public b_UserInfodata getuserinfobySchool(string  school)
        {
            //根据学校搜索出符合条件的用户信息
            b_UserInfodata newdata = new b_UserInfodata();
            string selectstring = "SELECT * FROM b_UserInfo WHERE User_School =" + "'" + school.ToString () + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_UserInfo");
            tempconn.Close();
            return (newdata);
        }
        public bool checkuserinfobyID(string username)
        {
            //根据用户名检察用户是否存在对应个人基本信息
            b_UserInfodata newdata = getuserinfobyID(username);
            if (newdata.Tables["b_UserInfo"].Rows.Count == 0)
                return false;
            else
                return true;
        }

        public bool deleteuserinfo(string username)
        {
            //根据用户名删除对应的用户个人基本信息
            b_UserInfodata newdata = new b_UserInfodata();
            string selectstring = "SELECT * FROM b_UserInfo WHERE User_ID =" + "'" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_UserInfo");

            if (newdata.Tables["b_UserInfo"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_UserInfo"].Rows[0].Delete();
                dscommand.Update(newdata, "b_UserInfo");
                tempconn.Close(); 
                return (true);
            }
                        
        }

        public bool modifyuserinfo(b_UserInfodata newdata)
        {
            //输入一个用户信息，修改对应用户注册邮箱处的个人信息为新信息
            string userID = newdata.Tables["b_UserInfo"].Rows[0][0].ToString();
            if (checkuserinfobyID (userID))
            {

                deleteuserinfo(userID);
                insertuserinfo(newdata);
                return true;
            }
            else
                return false;
        }

        public bool insertuserinfo(b_UserInfodata newdata)
        {
            //输入一个用户信息，插入到数据库中去
            if (newdata.Tables["b_UserInfo"].Rows.Count == 0)
                return false;
            else
            {
                string insertuserID = newdata.Tables["b_UserInfo"].Rows[0]["User_ID"].ToString();
                if (checkuserinfobyID(insertuserID))
                    return false;
                else
                {
                    string selectstring = "SELECT * FROM b_UserInfo";
                    b_UserInfodata testdata = new b_UserInfodata();
                    dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
                    SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
                    SqlCommand selcmd = dscommand.SelectCommand;
                    SqlConnection tempconn = selcmd.Connection;
                    tempconn.Open();
                    dscommand.Fill(testdata, "b_UserInfo");
                    DataRow newrow = testdata.Tables["b_UserInfo"].NewRow();
                    int i;
                    for (i = 0; i < newdata.Tables["b_UserInfo"].Columns.Count; i++)
                    {
                        newrow[i] = newdata.Tables["b_UserInfo"].Rows[0][i];
                    }
                    testdata.Tables["b_UserInfo"].Rows.Add(newrow);
                    dscommand.Update(testdata, "b_UserInfo");
                    tempconn.Close();
                    return (true);
                }
            }
        }

        public b_UserInfodata returnalldata()
        {
            //返回所有的用户基本信息
            //用户取得最近的五个用户的昵称
            b_UserInfodata newdata = new b_UserInfodata();
            string selectstring = "SELECT * FROM b_UserInfo ORDER BY User_Logdate ASC";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_UserInfo");
            tempconn.Close();
            return (newdata);
        }

  

    }
}
